package com.dcivision.ldap.core;

import java.sql.Connection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

public class LdapImportJob implements Job {

  protected Log log = LogFactory.getLog(this.getClass().getName());

  public void execute(JobExecutionContext arg0) throws JobExecutionException {
  log.info("start ldap backend import");
  JobDataMap dataMap = arg0.getJobDetail().getJobDataMap();
  Connection conn = null;
  SessionContainer sessionContainer = null;

  try {
      sessionContainer = new SessionContainer();
      conn = DataSourceFactory.getConnection();

      UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionContainer, conn);
      sessionContainer.setUserRecord((UserRecord)userRecordDAO.getObjectByID(new Integer("0")));

      sessionContainer.setByPassPermission(true);
      sessionContainer.setUserIPAddress("127.0.0.1");

      Env env =new Env();
      LdapImportManager operator = new LdapImportManager(sessionContainer,conn,env);
      operator.importData(true,true,true);

      conn.commit();
    } catch (Exception e) {
      try {conn.rollback();}catch (Exception ignore) {}
      log.error(e,e);
    } finally {
      try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
    }
  }


}

